package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.R_Code;
import com.yc.Result;
import com.yc.entities.BloodSugarQualityControl;
import com.yc.entities.BloodSugarQualityRecords;
import com.yc.entities.custom.CustomQualityRecordsResponseBean;
import com.yc.service.BloodSugarArticleTryService;
import com.yc.service.BloodSugarGluceProgramService;
import com.yc.service.BloodSugarQualityControlService;
import com.yc.service.BloodSugarQualityRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * <p>
 * 质控检测记录表 前端控制器
 * </p>
 *
 * @author kuakha
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/bloodSugarQualityRecords")
@Api(value = "BloodSugarArticleTryController", tags = {"质控检测记录"})
public class BloodSugarQualityRecordsController {

    @Autowired
    private BloodSugarQualityRecordsService bloodSugarQualityRecordsService;
    @Autowired
    private BloodSugarQualityControlService bloodSugarQualityControlService;
    @Autowired
    private BloodSugarArticleTryService bloodSugarArticleTryService;

    @ApiOperation(value = "APP添加质控检测")
    @PostMapping("insertQualityRecord")
    public Result insertQualityRecord(BloodSugarQualityRecords bloodSugarQualityRecords) {
        Integer equipmentId = bloodSugarQualityRecords.getEquipmentId();
        if (equipmentId == null || equipmentId == 0) {
            return Result.error().code(301).message("请选择设备");
        }
        Integer qualityControlId = bloodSugarQualityRecords.getQualityControlId();
        if (qualityControlId == null || qualityControlId == 0) {
            return Result.error().code(302).message("请选择质控批号");
        }
        Double detectionValue = bloodSugarQualityRecords.getDetectionValue();
        if (detectionValue == null || detectionValue == 0) {
            return Result.error().code(303).message("检测值不能为空");
        }
        Integer userId = bloodSugarQualityRecords.getUserId();
        if (userId == null || userId == 0) {
            return Result.error().code(304).message("userId不能为空");
        }
        QueryWrapper<BloodSugarQualityControl> query = Wrappers.query();
        query.eq("id", qualityControlId);
        query.eq("department_id", bloodSugarQualityRecords.getDepartmentId());
        BloodSugarQualityControl one = bloodSugarQualityControlService.getOne(query);
        if (one != null) {
            Double qualityMax = one.getQualityMax();
            Double qualityMin = one.getQualityMin();
            if (detectionValue > qualityMax) {
                bloodSugarQualityRecords.setDetectionStatus("2");
            } else if (detectionValue < qualityMin) {
                bloodSugarQualityRecords.setDetectionStatus("0");
            } else {
                bloodSugarQualityRecords.setDetectionStatus("1");
            }
        }
        bloodSugarQualityRecords.setAddTime(new Date());
        boolean save = bloodSugarQualityRecordsService.save(bloodSugarQualityRecords);
        if (save) {
            return Result.ok();
        } else {
            return Result.error().code(300).message("操作失败");
        }
    }

    @ApiOperation(value = "pc查询所有的质控检测记录")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping("getQualityControl")
    public Result getQualityRecords(@ApiParam(name = "deanId", value = "院长id") @RequestParam Integer deanId,
                                    @ApiParam(name = "departmentId", value = "科室id") @RequestParam(required = false) Integer departmentId,
                                    @ApiParam(name = "startTime", value = "起始时间") @RequestParam(required = false) String startTime,
                                    @ApiParam(name = "endTime", value = "结束时间") @RequestParam(required = false) String endTime,
                                    @ApiParam(name = "current", value = "当前页码") @RequestParam(required = false) Integer current,
                                    @ApiParam(name = "size", value = "一页显示多少条") @RequestParam(required = false) Integer size) {
        return bloodSugarQualityRecordsService.getQualityRecords(deanId, departmentId, startTime, endTime, current, size);
    }

    @ApiOperation(value = "pc添加质控检测记录")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @PostMapping("addQualityRecords")
    public Result addQualityRecords(@Valid @RequestBody BloodSugarQualityRecords bloodSugarQualityRecords) {
        LambdaQueryWrapper<BloodSugarQualityControl> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(BloodSugarQualityControl::getId, bloodSugarQualityRecords.getQualityControlId());
        BloodSugarQualityControl bloodSugarQualityControl = bloodSugarQualityControlService.getOne(objectLambdaQueryWrapper);
        if (bloodSugarQualityRecords.getDetectionValue() > bloodSugarQualityControl.getQualityMax()) {
            bloodSugarQualityRecords.setDetectionStatus("2");
        } else if (bloodSugarQualityRecords.getDetectionValue() < bloodSugarQualityControl.getQualityMin()) {
            bloodSugarQualityRecords.setDetectionStatus("1");
        } else {
            bloodSugarQualityRecords.setDetectionStatus("0");
        }
        boolean save = bloodSugarQualityRecordsService.save(bloodSugarQualityRecords);
        if (save) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_ADD_FAIL);
        }
    }

    @ApiOperation(value = "pc修改质控检测记录")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @PostMapping("updateQualityRecords")
    public Result updateQualityRecords(@Valid @RequestBody BloodSugarQualityRecords bloodSugarQualityRecords) {
        Integer equipmentId = bloodSugarQualityRecords.getEquipmentId();
        if (equipmentId == null || equipmentId == 0) {
            return Result.error().code(301).message("请选择设备");
        }
        Double detectionValue = bloodSugarQualityRecords.getDetectionValue();
        if (detectionValue == null || detectionValue == 0) {
            return Result.error().code(303).message("检测值不能为空");
        }
        Integer userId = bloodSugarQualityRecords.getUserId();
        if (userId == null || userId == 0) {
            return Result.error().code(304).message("userId不能为空");
        }
        LambdaQueryWrapper<BloodSugarQualityControl> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(BloodSugarQualityControl::getId, bloodSugarQualityRecords.getQualityControlId());
        BloodSugarQualityControl bloodSugarQualityControl = bloodSugarQualityControlService.getOne(objectLambdaQueryWrapper);
        if (bloodSugarQualityRecords.getDetectionValue() > bloodSugarQualityControl.getQualityMax()) {
            bloodSugarQualityRecords.setDetectionStatus("2");
        } else if (bloodSugarQualityRecords.getDetectionValue() < bloodSugarQualityControl.getQualityMin()) {
            bloodSugarQualityRecords.setDetectionStatus("1");
        } else {
            bloodSugarQualityRecords.setDetectionStatus("0");
        }
        boolean update = bloodSugarQualityRecordsService.updateById(bloodSugarQualityRecords);
        if (update) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_UPDATE_FAIL);
        }
    }


    @ApiOperation(value = "pc打印质控检测记录")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping("getAllRecordsPrintInfo")
    public Result getAllRecordsPrintInfo(@ApiParam(name = "deanId", value = "院长id") @RequestParam Integer deanId,
                                         @ApiParam(name = "departmentId", value = "科室id") @RequestParam(required = false) Integer departmentId,
                                         @ApiParam(name = "startTime", value = "起始时间") @RequestParam(required = false) String startTime,
                                         @ApiParam(name = "endTime", value = "结束时间") @RequestParam(required = false) String endTime) {

        return bloodSugarQualityRecordsService.getQualityRecord(deanId, departmentId, startTime, endTime);
    }

    @ApiOperation(value = "pc查看行数据")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping("getRowDataById")
    public Result getRowDataById(@ApiParam(name = "id", value = "质控记录主键ID")@RequestParam String id) {
        if (id != null && !"".equals(id)) {
            Integer id1 = Integer.valueOf(id);
            BloodSugarQualityRecords byId = bloodSugarQualityRecordsService.getById(id1);
            return Result.ok().data(byId);
        }
        return Result.error().message("没有查询到数据");
    }


}

